package com.example.demo.controller;

import com.example.demo.mapper.StudentMapper;
import com.example.demo.pojo.Student;
import com.example.demo.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin /*允许跨域访问*/
@RestController
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @RequestMapping("/findAll")
    public Result findAll(@RequestParam("keyword") String keyword,
                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentMapper.findAll(keyword);
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        Result result = new Result(200, "查询成功", pageInfo);
        return result;
    }


    @RequestMapping("/saveStudent")
    public int saveStudent(@RequestBody Student student) {
        return studentMapper.saveStudent(student);
    }
    @RequestMapping(value = "/updateStudent", method = RequestMethod.POST)
    public int updateStudent(@RequestBody Student student) {
        return studentMapper.updateStudent(student);
    }
    @RequestMapping(value = "/deleteStudent", method = RequestMethod.GET)
    public int deleteStudent(int id) {
        return studentMapper.deleteStudent(id);
    }
}
